# 查询

"""
1.创建会话
"""
from sqlalchemy.orm import sessionmaker
from create_table_foreignkey import engine
Session = sessionmaker(engine)
db_session = Session()

from create_table_foreignkey import School, Student

"""
1. 正向查询
"""
stu = db_session.query(Student).all()
for row in stu:
    print(row.id,row.name,row.school_id,row.stu2sch.name)

"""
2. 反向查询
"""

sch = db_session.query(School).all()
for row in sch:
    for row2 in row.sch2stu:
        print(row2.id,row2.name,row.id,row.name)